package cn.yd.rb.service.serviceimpl;

import cn.yd.rb.exception.CustomException;
import cn.yd.rb.mapper.UserMapper;
import cn.yd.rb.po.User;
import cn.yd.rb.po.UserExample;
import cn.yd.rb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    /**
     *
     */
    @Autowired
    private UserMapper userMapper;

    @Override
    public void regist(User user) {
        user.setIsVip(0);
        userMapper.insertSelective(user);
    }

    @Override
    public User findUserByLogin(User user) throws Exception {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        List<User> list = userMapper.selectByExample(userExample);
        if(list.size()<=0){
            throw new CustomException("无该用户");
        }
        return list.get(0);
    }

    @Override
    public List<User> findAllUsers() {
        UserExample userExample=new UserExample();
        List<User> userList=userMapper.selectByExample(userExample);
        return  userList;
    }
    @Override
    public User findUserByForget1(User user) throws Exception{
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        List<User> list = userMapper.selectByExample(userExample);


        if(list.size()<=0){
            throw new CustomException("无该用户");
        }


        return list.get(0);
    }



    @Override
    public User findUserByForget2(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andPhoneEqualTo(user.getPhone());
        List<User> list = userMapper.selectByExample(userExample);
        if(list.size()<=0){
            throw new CustomException("手机号不正确");
        }


        return list.get(0);
    }

    @Override
    public void findUserByForget3(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());

        List<User> list  = userMapper.selectByExample(userExample);

        list.get(0).setPassword(user.getPassword());
        userMapper.updateByPrimaryKeySelective(list.get(0));

    }
    @Override
    public Integer findUserCounts() {
        UserExample userExample=new UserExample();
        Integer userCounts=userMapper.selectByExample(userExample).size();
        return  userCounts;
    }

    @Override
    public User findUserByAjax(String username) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        User user=new User();
        if(userMapper.selectByExample(userExample).size()!=0){
            user = userMapper.selectByExample(userExample).get(0);
            return user;
        }
        else{
            return null;
        }
    }

    public User findUserByBevip(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());

        List<User> list  = userMapper.selectByExample(userExample);

        list.get(0).setIsVip(1);
        userMapper.updateByPrimaryKeySelective(list.get(0));
         list  = userMapper.selectByExample(userExample);
         return list.get(0);
    }
}
